package demo;


import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Student {
	private boolean isInOut = false;
	private Integer id;
	private String firstName;
	private String lastName;
	private BigDecimal math;
	private BigDecimal phys;
	private BigDecimal chem;
	private boolean isUsed = false;
	//private Connection con;
	
	public Student(Integer ID, String firstName, String lastName, BigDecimal math, BigDecimal phys, BigDecimal chem, boolean isUsed) {
		this.id = ID;
		this.firstName = firstName;
		this.lastName = lastName;
		this.math = math;
		this.phys = phys;
		this.chem = chem;
		this.isUsed = isUsed;
	}

	public Student(Integer p_Student_ID) throws Exception {
		String sql = "select * from p_student where p_student_id = ?";
		Connection con = CreateConnection.getConnection();
		if (con == null) {
			id = 0;
			firstName = "";
			lastName = "";
			math = BigDecimal.ZERO;
			phys = BigDecimal.ZERO;
			chem = BigDecimal.ZERO;
		} else {
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				ps = con.prepareStatement(sql);
				ps.setInt(1, p_Student_ID);
				rs = ps.executeQuery();
				if (rs.next()) {
					id = rs.getInt("P_Student_ID");
					firstName = rs.getString("firstName");
					lastName = rs.getString("lastName");
					math = rs.getBigDecimal("Math");
					phys = rs.getBigDecimal("Phys");
					chem = rs.getBigDecimal("Chem");
					String used = rs.getString("IsUsed");
					if (used.equals("Y")) {
						isUsed = true;
					}
				} else {
					id = 0;
					firstName = "";
					lastName = "";
					math = BigDecimal.ZERO;
					phys = BigDecimal.ZERO;
					chem = BigDecimal.ZERO;
					isUsed = false;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (rs != null) {
					rs.close();	
				}
				if (ps != null) {
					ps.close();
				}
				con.close();
			}

		}
	}

	public Integer getP_Student_ID() {
		return id;
	}

	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName(){
		return lastName;
	}
	
	public BigDecimal getMathermatic(){
		if (math == null) {
			return BigDecimal.ZERO;
		}
		return math;
	}
	
	public BigDecimal getPhysical(){
		if (phys == null) {
			return BigDecimal.ZERO;
		}
		return phys;
	}
	
	public BigDecimal getChemistry(){
		if (chem == null) {
			return BigDecimal.ZERO;
		}
		return chem;
	}
	
	public boolean getIsUsed(){
		return isUsed;
	}
	
	public void setIsUsed(boolean isUsed) {
		this.isUsed = isUsed;
	}
	
	/**
	 * 
	 * @return true neu dang o trong queue input
	 * @return false neu dang o ngoai queue input
	 * 
	 */
	public boolean getIsInPOut(){
		return isInOut;
	}
	
	public void setIsInProcess(boolean isInOut) {
		this.isInOut = isInOut;
	}
	
	public boolean save() {
		boolean isSave = false;
		StringBuffer sql = new StringBuffer();
		sql.append("update P_STUDENT set FIRSTNAME = ?");
		sql.append(", LASTNAME = ?");
		sql.append(", MATH = ?");
		sql.append(", PHYS = ?");
		sql.append(", CHEM = ?");
		if (isUsed) {
			sql.append(", ISUSED = 'Y'");
		} else
			sql.append(", ISUSED = 'N'");
		sql.append(" where P_STUDENT_ID = ?");
		PreparedStatement ps = null;
		Connection con = null;
		try {
			con = CreateConnection.getConnection();
			if (con == null) {
				return false;
			}
			ps = con.prepareStatement(sql.toString());
			ps.setString(1, firstName);
			ps.setString(2, lastName);
			ps.setBigDecimal(3, math);
			ps.setBigDecimal(4, phys);
			ps.setBigDecimal(5, chem);
			ps.setInt(6, id);
			int i = ps.executeUpdate();
			if (i >= 0) {
				isSave = true;
				con.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return isSave;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Student) {
			Student studentTmp = (Student)obj;
			if (studentTmp.getP_Student_ID() == id) {
				return true;
			}
		}
		return super.equals(obj);
	}
	
	@Override
	public String toString() {
		BigDecimal avg = math.add(chem).add(phys).divide(new BigDecimal(3), 2, BigDecimal.ROUND_HALF_UP);
		StringBuffer value = new StringBuffer();
		value.append(id).append("___");
		value.append(firstName).append("___").append(lastName).append("---").append(avg);
		return value.toString();
	}
}